/**
 * 
 */
package com.javass.jdbc.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.mybatis.generator.internal.util.StringUtility;

import com.javass.jdbc.DatabaseComment;

/**
 * @author zhuguojie，zhu915139481@126.com
 * @date 2015年11月11日 下午4:35:09
 * @version 1.0
 */
public class OracleDatabaseComment extends AbstractDatabaseComment implements DatabaseComment {

	public OracleDatabaseComment(Connection connection) {
		super(connection);
	}

	@Override
	public String getTableComment(String catalog, String schema, String tableName) throws SQLException {
		String comment = super.getTableComment(null, schema, tableName);
		if (!StringUtility.stringHasValue(comment)) {
			PreparedStatement ps = null;
			ps = connection
					.prepareStatement("select a.TABLE_NAME,b.COMMENTS from user_tables a,user_tab_comments b  WHERE a.TABLE_NAME=b.TABLE_NAME and a.TABLE_NAME=?");
			ps.setString(1, tableName.toUpperCase());

			ResultSet resultSet = ps.executeQuery();
			if (resultSet.next()) {
				comment = resultSet.getString(2);
			}
			resultSet.close();
			ps.close();
		}
		closeConnection();
		return comment;
	}

	@Override
	public String getColumnComment(String catalog, String schema, String tableName, String columnName) throws SQLException {
		String comment = super.getColumnComment(null, schema, tableName, columnName);
		if (!StringUtility.stringHasValue(comment)) {
			PreparedStatement ps = null;
			ps = connection
					.prepareStatement("select b.TABLE_NAME,b.COLUMN_NAME,b.comments from user_col_comments b  WHERE b.TABLE_NAME=? and b.COLUMN_NAME=?");
			ps.setString(1, tableName.toUpperCase());
			ps.setString(2, columnName.toUpperCase());

			ResultSet resultSet = ps.executeQuery();
			if (resultSet.next()) {
				comment = resultSet.getString(3);
			}
			resultSet.close();
			ps.close();
		}
		closeConnection();
		return comment;
	}

}
